<?php
include_once('includes.php'); 	
//------------------------------------------------------------------------------------------

//------------------------------------------------------------------------------------------

$sql = "SELECT matchs.id, matchs.url FROM matchs  LEFT JOIN highlights ON highlights.match_id = matchs.id WHERE matchs.status=2 and highlights.id IS NULL order by matchs.kick_off desc";
$matchs=db_fetch_all($sql);
foreach($matchs as $match)
{
	$url= trim($match->url);
	if($url=="")
		continue;
	echo "<strong>".$url."</strong><br/>";
	
	$ch = curl_init($url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($ch, CURLOPT_HEADER, 0);
	$html = curl_exec($ch);
	curl_close($ch);
	//-----------------------
		
	$patterns = array();
	$patterns[0] = '/<tbody>/';
	$patterns[1] = '/<\/tbody>/';
	$patterns[2] = '/<strong>/';
	$patterns[3] = '/<\/strong>/';
	$html = preg_replace($patterns, '', $html);

	//----------------------------	
	$dom = new DOMDocument();
	@$dom->loadHTML($html);
	$xpath = new DOMXPath($dom);

	//get score------------------------------------
	$elements_result = $xpath->query('//table[@class="contentpaneopen"]/tr/td[@class="contentheading"]');
	if (!is_null($elements_result)) {
		$result = trim($elements_result->item(0)->nodeValue);
		if($result=="")
			continue;
		echo $result."<br/>";
		preg_match('/([0-9]+)\-([0-9]+)/', $result, $result_matches);
		$score = trim($result_matches[0]);
		if($score=="")
			continue;
	}
	
	//get highlight------------------------------------
	$elements_highlight = $xpath->query('//table[@id="customers"]');
	if (!is_null($elements_highlight)) {
		$e_highlight1 = $elements_highlight->item(1)->childNodes->item(1)->childNodes->item(0);
		$e_highlight2 = $elements_highlight->item(1)->childNodes->item(2)->childNodes->item(0);
		
		
		
		$highlight1 = DOMinnerHTML($e_highlight1) ; 
		$highlight2 = DOMinnerHTML($e_highlight2) ; 
		$highlight1 = str_replace("Highlights:","",$highlight1);
		
		//remove overlayVideo ads
		$highlight1 = preg_replace('~<div class="hideLink">[^|]*?</div>~', '', $highlight1);
		$highlight1 = preg_replace('~<div class="videoBoxContainer">[^|]*?</div>~', '', $highlight1);
		$highlight1 = preg_replace('~<div class="overlayColor">[^|]*?</div>~', '', $highlight1);
		$highlight1 = preg_replace('~<div class="overlayVid" id="overlayVid">[^|]*?</div>~', '', $highlight1);
		
		//Change width and height video		
		$patterns = array();
		$patterns[0] = '/width="([0-9]+)"/';
		$patterns[1] = '/height="([0-9]+)"/';
		$replacements = array();
		$replacements[0] = 'width="600"';
		$replacements[1] = 'height="400"';
		$highlight1 = preg_replace($patterns, $replacements, $highlight1);
		
	
		

		$highlight2 = str_replace("Final","",$highlight2);
		$highlight_content = $highlight1."<div>".$highlight2."<div>"; 		
		//echo $highlight_content ;
	}
	
	//-----------------------		
	
	$data["match_id"] = $match->id;
	$data["score"] = $score;
	$data["content"] = $highlight_content;
	if (get_magic_quotes_gpc()) {
		$data["content"] = stripslashes($highlight_content);
	}
	db_insert("highlights",$data);		
	
	
}


//-----------------------		
	
function DOMinnerHTML($element)
{
    $innerHTML = "";
    $children = $element->childNodes;
    foreach ($children as $child)
    {
        $tmp_dom = new DOMDocument();
        $tmp_dom->appendChild($tmp_dom->importNode($child, true));
        $innerHTML.=trim($tmp_dom->saveHTML());
    }
    return $innerHTML;
} 

?>